package com.xb.loan.cashier.biz.task.unionpay;

import com.xb.loan.cashier.biz.dao.bean.CashierPayChannel;
import com.xb.loan.cashier.biz.pay.unionpay.UnionPayment;
import com.xb.loan.cashier.biz.pay.unionpay.util.UnionPayUtil;
import com.xb.loan.cashier.biz.task.ThirdAccountChargeTask;
import com.xb.loan.cashier.biz.task.bean.ThirdAccountBean;
import com.xb.loan.cashier.biz.task.bean.ThirdAccountChargeBean;
import com.xb.loan.cashier.biz.util.FileUtil;
import com.xb.loan.cashier.service.constant.ICashierEnumConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.io.File;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 银联充值对账任务

 */
@Component(value="unionPayAccountChargeTask")
@Scope(value="prototype")
public class UnionPayAccountChargeTask extends ThirdAccountChargeTask {
    
    private static final Logger logger = LoggerFactory.getLogger(UnionPayAccountChargeTask.class);


    @Override
    protected boolean hasThirdAccountTime() {
        return true;
    }

    @Override
    protected int getPayChannelType() {
        return ICashierEnumConst.CashierPayChannelType.UNIONPAY.getType();
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
    protected Map<String, ThirdAccountBean> getThirdAccountDatas(int pageNo, int pageSize, String day, CashierPayChannel channel) {
    	if (pageNo==2) {
			return null;
		}
    	String filePath = "";
    	String targetDir = "";
    	HashMap<String, ThirdAccountBean> result = new HashMap<String, ThirdAccountBean>();
        String merId = channel.getChannelUser();
        String settleDate = day.substring(4, 8);
        String url = channel.getQueryUrlBatch();
        logger.info("getThirdAccountDatas day="+day+",merId="+merId+",url="+url);
        File file = UnionPayment.downloadBill(merId, settleDate, url);
        if (file==null) {
			return result;
		}
        targetDir = file.getAbsolutePath().replace(".zip", "");
        FileUtil.unzipFile(file.getAbsolutePath(), targetDir);
        if (file.getParentFile().isDirectory()) {
			for (File f: new File(targetDir).listFiles()) {
				if (f.getName().contains("ZM")) {
					filePath = f.getAbsolutePath();
					break;
				}
			}
			List<Map> list = UnionPayUtil.parseZMFile(filePath);
			logger.info("getThirdAccountDatas fileContent rowSize:"+list.size());
			for (Map<Integer,String> map : list) {
				 ThirdAccountChargeBean order = new ThirdAccountChargeBean();
				 double money = Double.parseDouble(new DecimalFormat("#0.00").format(Double.valueOf(map.get(6))/100));
				 order.setMoney(money);
	    		 order.setOrderIdThird(map.get(9));
	             order.setOrderId(map.get(11));
//	             order.setBuyAccount(buyer_account);TODO:没找到匹配字段
//				 order.setAccountDayThird();TODO:没找到匹配字段
	             order.setAccountDay(null);
	             order.setStatus(ThirdAccountBean.STATUS_SUCCESS);
	             result.put(order.getOrderId(), order);
			}
		}
        
        return result;
    }
    
    public static void main(String[] args) {
		UnionPayAccountChargeTask task = new UnionPayAccountChargeTask();
		CashierPayChannel channel = new CashierPayChannel();
		channel.setChannelUser("700000000000001");
		channel.setQueryUrlBatch("https://101.231.204.80:9080/");
		Map<String, ThirdAccountBean> map = task.getThirdAccountDatas(0, 0, "20150119", channel);
		System.out.println("===="+map.size());
	}
    
}
